package com.aiitec.camera;

import android.Manifest;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.view.ViewGroup;
import android.widget.LinearLayout;

public class WelcomeActivity extends AppCompatActivity {
    private static final int PERMISSION_REQUEST_WRITE_SETTING = 1;
    private static final int PERMISSION_CAMERA = 2;
    private PermissionsUtils permissionsUtils;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        LinearLayout linearLayout = new LinearLayout(this);
        ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        linearLayout.setLayoutParams(params);
        setContentView(linearLayout);
        permissionsUtils = new PermissionsUtils(this);

        permissionsUtils.setOnPermissionsListener(new PermissionsUtils.OnPermissionsListener() {
            @Override
            public void onPermissionsSuccess(int requestCode) {
                switch (requestCode){
                    case PERMISSION_REQUEST_WRITE_SETTING:
//                        try {
//                            Settings.System.putInt(getContentResolver(), Settings.System.SOUND_EFFECTS_ENABLED,  0);
//                            mAudioManager.unloadSoundEffects();
//                        } catch (Exception e){
//                            e.printStackTrace();
//                        }
                        ToastUtil.show(WelcomeActivity.this, "权限获取失败");

                        break;
                    case PERMISSION_CAMERA:
//                        ToastUtil.show(WelcomeActivity.this, "权限获取成功");
//                        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP){
//                            startActivity(new Intent(WelcomeActivity.this, MainActivity.class));
//                        } else {
                            startActivity(new Intent(WelcomeActivity.this, LiveCameraActivity.class));
//                        }
                        finish();
                        break;
                    default:
                            break;
                }
            }

            @Override
            public void onPermissionsFailure(int requestCode) {
                switch (requestCode){
                    case  PERMISSION_REQUEST_WRITE_SETTING:

                        break;
                    case PERMISSION_CAMERA:

                        break;
                        default:
                            break;
                }
            }
        });
        permissionsUtils.requestPermissions(PERMISSION_CAMERA, Manifest.permission.CAMERA);

    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        permissionsUtils.onRequestPermissionsResult(requestCode, permissions, grantResults);
    }
}
